package artid.com.arttogo.activity;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.HitTestResult;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import artid.com.arttogo.R;
import artid.com.arttogo.adapter.ListAdapter;
import artid.com.arttogo.adapter.OrderListAdapter;
import artid.com.arttogo.bean.GcGoodslist;
import artid.com.arttogo.bean.OrderList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * TODO<请描述这个类是干什么的> 订单列表
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class OrderListActivity extends BaseActivity implements OnClickListener {
    private Context mContext;
    private PullableListView mListView;
    private OrderListAdapter adapter;
    private List<OrderList> list;
    private TextView title;
    private ListAdapter typeAdapter;
    PullToRefreshLayout mPullLayout;
    int pageIndex = 1;
    String strType = "";
    private ImageLoader mImageLoader;
    String pagesize = "20";
    private LinearLayout mLinearLayout;
    /** 屏幕宽度 */
    private int mScreenWidth;
    /** 导航条目宽度 */
    private int item_width;
    List<GcGoodslist> typeList;
    int endPosition = 0, beginPosition, currentFragmentIndex;
    ImageView mImageView;
    /** 导航栏 */
    private HorizontalScrollView mHorizontalScrollView;
    LinearLayout blankView;
    /*
     * (non-Javadoc)
     *
     * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderlist);
        blankView = (LinearLayout)findViewById(R.id.ll_hint);
        ImageView imageBlank = (ImageView)blankView.findViewById(R.id.img_blank);
        imageBlank.setImageResource(R.drawable.blank_order);
        ViewGroup.LayoutParams lp = imageBlank.getLayoutParams();
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        imageBlank.setLayoutParams(lp);
        blankView.setVisibility(View.GONE);
        TextView textBlank = (TextView)blankView.findViewById(R.id.text_blank);
        textBlank.setText("您还没有相关订单");
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("订单列表");
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        mContext = OrderListActivity.this;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        item_width = (int) ((mScreenWidth / 4.0 + 0.5f));
        mImageView = (ImageView) findViewById(R.id.img1);
        mImageView.getLayoutParams().width = item_width;
        mListView = (PullableListView) findViewById(R.id.listivew);
        title = (TextView) findViewById(R.id.tv_text);
        RequestQueue mQueue = Volley.newRequestQueue(mContext);
        mImageLoader = new ImageLoader(mQueue, new BitmapCache());
        mLinearLayout = (LinearLayout) findViewById(R.id.hsv_content);
        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv_view);
        loading = (RelativeLayout) findViewById(R.id.rel_progressbar);
        loading.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                System.out.println("#zhebiceng");
            }
        });

        mPullLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                pageIndex = 1;
                reqData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // pullToRefreshLayout.setBottomDismiss();
                pageIndex++;
                reqData();
            }
        });

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initNav();
        reqData();
    }
    private void initNav() {
        String[] arrTag = { "全部", "未支付", "未发货", "已发货", "已完成" };
        mLinearLayout.removeAllViews();
        for (int i = 0; i < arrTag.length; i++) {
            RelativeLayout layout = new RelativeLayout(this);
            TextView view = new TextView(this);
            view.setText(arrTag[i]);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            layout.addView(view, params);
            mLinearLayout.addView(layout, item_width, 50);
            layout.setOnClickListener(this);
            layout.setTag(i);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        switch (position) {
            case 0:
                strType = "";
                break;
            case 1:
                strType = "1";
                break;
            case 2:
                strType = "2";
                break;
            case 3:
                strType = "3";
                break;
            case 4:
                strType = "4";
                break;
            default:
                break;
        }

        Animation animation = new TranslateAnimation(endPosition * item_width,
                position * item_width, 0, 0);

        beginPosition = position * item_width;

        currentFragmentIndex = position;
        if (animation != null) {
            animation.setFillAfter(true);
            animation.setDuration(0);
            mImageView.startAnimation(animation);
            mHorizontalScrollView.smoothScrollTo((currentFragmentIndex - 1)
                    * item_width, 0);
        }
        endPosition = position;

        reqData();
    }

    View headView = null;
    JSONObject mainJson;
    JSONArray mainJSONArr;

    private void reqData() {
        VolleyBasic.getInstance(OrderListActivity.this).post(
                Cache.orderList,
                VolleyMap.getInstance().orderList(String.valueOf(pageIndex),
                        pagesize, strType), OrderList.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
                        list = (List<OrderList>) vd.getList();
                        JSONObject jsonString = vd.getObj();
                        JSONArray jsonArray = jsonString.optJSONArray("data");
//						list = new ArrayList<OrderList>();
                        if (list.isEmpty()) {
                            if (pageIndex == 1) {
                                mListView.setVisibility(View.INVISIBLE);
                                blankView.setVisibility(View.VISIBLE);
                            }
                            return;
                        }
                        blankView.setVisibility(View.GONE);
                        if (mListView.getVisibility() == View.INVISIBLE) {
                            mListView.setVisibility(View.VISIBLE);
                        }
                        if (pageIndex != 1) {
                            try {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    mainJSONArr.put(jsonArray.get(i));
                                }
                                mainJson.remove("data");
                                mainJson.put("data", mainJSONArr);
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            adapter.addlist(list);
                            setItemClick();
                            return;
                        }
                        mainJson = jsonString;
                        mainJSONArr = jsonArray;
                        adapter = new OrderListAdapter(mContext);
                        adapter.setdata(list);
                        mListView.setAdapter(adapter);
                        setItemClick();

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                    }
                });
    }

    void setItemClick() {
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, OrderDetActivity.class);
                intent.putExtra("id", adapter.listdata.get(arg2).getOrder_id());
                startActivity(intent);
            }

        });
    }

}
